package com.csdj.t309springboot.controller;
import com.csdj.t309springboot.constant.SessionConstant;
import com.csdj.t309springboot.entity.User;
import com.csdj.t309springboot.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
@Controller
public class UserController {
    @Resource
    private UserService userService;

    private Logger log = LoggerFactory.getLogger("test");

    /**
     * 作者的详情页面
     * @return
     */

    @GetMapping("details")
    public String details(String username,String title,String dataTime){

        return "details_index";
    }


    @GetMapping("publish")
    public String publish(HttpServletRequest request,Model model){
        HttpSession session = request.getSession();
        User user =(User) session.getAttribute(SessionConstant.USER_SESSION);
        log.info("username="+user.getName());
        model.addAttribute(SessionConstant.USERNAME_IN_SESSION,user.getName());
        return "publish";
    }

//    @GetMapping("login")
//    public String toLogin(){
//        log.info("toLogin");
//        //转到login.html
//        return "login2";
//    }

    @PostMapping("doLogin")
    public String doLogin(/*@RequestParam */String account, String password, Model model,
                          HttpServletRequest request, HttpServletResponse response, HttpSession session)
            throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        log.info("doLogin()==, account="+account+",password="+password);
        PrintWriter out = response.getWriter();
        User user = userService.login(account, password);
        if(null != user){ //
            session = request.getSession();
            session.setAttribute( SessionConstant.USER_SESSION,user);
            model.addAttribute("user",user);
            model.addAttribute("name",user.getName());

            log.info("doLogin() name="+user.getName());
            int roleId=user.getRoleId();
            log.info("登录成功,roleId="+roleId);
            if(roleId == 0){//是管理员
//                return "redirect:/menu/list";
                return "backend";
            }//否则是用户
            //重定向到首页 redirect:
            return "redirect:/topic/index";
        }
        //用户名或密码错误
        out.println("<script>alert('用户名或密码错误')</script>");
//        model.addAttribute("error","用户名或密码错误");
        return "login";
    }

    @GetMapping("/logout")
    public String logout(){
        return "redirect:login";
    }

    @PostMapping("doRegister")
    public String doRegister(String account, String password, Model model, HttpServletRequest request,
                             HttpSession session){
        log.info("doRegister()==, account="+account+",password="+password);
        User user = userService.register(account, password);
        if( null != user ){



            model.addAttribute("user",user);
            log.info("注册成功");
            //重定向到首页
            return "redirect:index";
        }
        model.addAttribute("error","用户名或密码错误");
        log.info("error");
        return "error";
    }

//    @GetMapping("index")
//    public String toIndex(){
//        return "index";
//    }

    @ResponseBody
    @GetMapping("test22")
    public String test(){
        return "SpringBoot Test";
    }

    @ResponseBody
    @GetMapping("/list2")
    public List<User> hello(){
        List<User> list=userService.getAllUsers();

        return list;
    }





}
